package com.yonyou.brigade.openapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.service.IUserCenterService;
import com.yonyou.brigade.openapi.service.IUserCenterListenerService;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/briUsercenterListener")
public class UserCenterListenerController {

    @Autowired
    @Qualifier("userCenterListenerServiceImpl")
    private IUserCenterListenerService iUserCenterListenerService;


    private Logger logger=LoggerFactory.getLogger(UserCenterListenerController.class);

    @Autowired
    @Qualifier("userCenterServiceImpl")
    private IUserCenterService userCenterService;


    @RequestMapping(value = "/addUserList/{tenantId}/{orgPk}",method = RequestMethod.POST)
    public JSONObject addUserList(@RequestBody  List<UcUserDto> ucUserDtoList,
                                   @PathVariable String tenantId,
                                   @PathVariable String orgPk){
        JSONObject result = new JSONObject();
        iUserCenterListenerService.addUserList(tenantId,orgPk,ucUserDtoList);
        result.put("code",0);
        result.put("information","新增成功");
        return result;
    }



    @RequestMapping(value = "/addUserListByOrg/{tenantId}/{orgPk}",method = RequestMethod.POST)
    public JSONObject addUserListByOrg(
            @PathVariable String tenantId,
            @PathVariable String orgPk){
        logger.info("开始添加用户，租户"+tenantId+",组织："+orgPk);
        JSONObject result = new JSONObject();
        List<UcUserDto> ucUserDtos= userCenterService.getUcUserDtoByOrg(tenantId,orgPk);
        logger.info("租户"+tenantId+",组织："+orgPk+"获取的用户数量为"+(ucUserDtos==null?"0":ucUserDtos.size()));
        iUserCenterListenerService.addUserList(tenantId,orgPk,ucUserDtos);
        result.put("code",0);
        result.put("information","新增成功");
        return result;
    }

    @RequestMapping(value = "/addUserList/{tenantId}/{phone}",method = RequestMethod.GET)
    public JSONObject addUserByPhone(@PathVariable  String phone,
                                     @PathVariable String tenantId){


        UcUserDto ucUserDto= userCenterService.getCompleteUcUserDtoByPhone(phone,tenantId);
        List<UcUserDto> ucUserDtoList =new ArrayList<>();
        ucUserDtoList.add(ucUserDto);
        JSONObject result = new JSONObject();
        iUserCenterListenerService.addUserList(tenantId,ucUserDto.getOrgPk(),ucUserDtoList);
        result.put("code",0);
        result.put("information","新增成功");
        return result;
    }


    @RequestMapping(value = "/modifyUserList/{tenantId}/{orgPk}",method = RequestMethod.POST)
    public JSONObject modifyUserList(@RequestBody  List<UcUserDto> ucUserDtoList,
                                   @PathVariable String tenantId,
                                   @PathVariable String orgPk){
        JSONObject result = new JSONObject();
        iUserCenterListenerService.modifyUserList(tenantId,orgPk,ucUserDtoList);
        result.put("code",0);
        result.put("information","修改成功");
        return result;
    }

    @RequestMapping(value = "/modifyUser/{tenantId}/{phone}",method = RequestMethod.GET)
    public JSONObject modifyUser(@PathVariable String tenantId,
                                 @PathVariable String phone){
        JSONObject result = new JSONObject();
        UcUserDto userDto=  userCenterService.getCompleteUcUserDtoByPhone(phone,tenantId);
        List<UcUserDto> ucUserDtoList=new ArrayList<>();
        ucUserDtoList.add(userDto);
        iUserCenterListenerService.modifyUserList(tenantId,userDto.getOrgPk(),ucUserDtoList);
        result.put("code",0);
        result.put("information","修改成功");
        return result;
    }

    @RequestMapping(value = "/deleteUserList/{tenantId}/{orgPk}",method = RequestMethod.POST)
    public JSONObject deleteUserList(@RequestBody  List<String> userIdList,
                                   @PathVariable String tenantId,
                                   @PathVariable String orgPk){
        JSONObject result = new JSONObject();
        iUserCenterListenerService.deleteUserList(tenantId,orgPk,userIdList);
        result.put("code",0);
        result.put("information","删除成功");
        return result;
    }

}
